/*******************************发送分页查询ajax请求******************************/
var currentPage = 1; //当前页
var pageSize = 5; //每页显示条数
var totalCount = 0; //总记录条数
var totalPage = 0; //总页数

function PageFind() {
    //1.获取输入框上的值
    var adminName = $('#adminName').val();
    var roleId = $('#roleId').val();
    var startTime = $('#startTime').val();
    var endTime = $('#endTime').val();
    //2.发送ajax 
    $.ajax({
        url: "admin/findAdminByPage",
        type: "post",
        dataType: "JSON",
        data: {
            adminName: adminName,
            roleId: roleId,
            startTime: startTime,
            endTime: endTime,
            currentPage: currentPage,
            pageSize: pageSize
        },
        success: function(pageBean) {
            //设置参数
            totalCount = pageBean.totalCount;
            totalPage = parseInt(pageBean.totalPage);
            //数据插入函数
            showTableData(pageBean.list);
        }
    })
};
//发送ajax
PageFind();

//定义数组
var stateName = { 1: '启用', 2: '禁用' };

function showTableData(dataList) {
    //1.设置当前页和总页数
    var pageNumberData = currentPage + '/' + totalPage;
    $('#pageNumber').html(pageNumberData);
    //2.设置表格数据
    //2.1清空表格
    $('#tableData').html('');
    //2.2遍历数据
    var trs = '';
    for (var i = 0; i < dataList.length; i++) {
        var dataDetail = dataList[i];
        //改日期
        var cDate = new Date(dataDetail.adminCtime).format('yyyy-MM-dd hh:mm:ss');
        var tr = `
                <tr>
                    <td hidden='hidden'>${dataDetail.adminId}</td>
                    <td>${dataDetail.adminName}</td>
                    <td>${cDate}</td>
                    <td hidden='hidden'>${dataDetail.roleId}</td>
                    <td>${dataDetail.roleName}</td>
                    <td>${stateName[dataDetail.adminState]}</td>
                    <td>
                        <button class="btn btn-mini " type="button" id='reSetPwd'>重置密码</button>
                        <button class="btn btn-mini " type="button" id='banPickBtn'>禁用/启用</button>
                        <button class="btn btn-mini " type="button" id='updateAdmin'>修改</button>
                    </td>
                </tr>     
                `;
        //添加数据
        trs += tr;
    }
    //2.3将数据加入
    $('#tableData').html(trs);
    //3.设置禁用启用按钮的事件
    $('[id = banPickBtn]').click(function() {
        var adminId = $(this).parent().parent().find('td').eq(0).html();
        var stateName = $(this).parent().parent().find('td').eq(5).html();
        banAjax(adminId, stateName);
    });
    //4.重置密码事件
    $('[id = reSetPwd]').click(function() {
        //adminId
        $('#rePwdAdminId').html($(this).parent().parent().find('td').eq(0).html());
        //设置账号
        $('#rePwdAccount').val($(this).parent().parent().find('td').eq(1).html());
        //清空隐藏
        $('#rePwdPwd').val('');
        //显示弹窗
        $('#rePwdModal').modal({
            backdrop: 'static', //点击页面不隐藏
            show: true, //显示窗口
            moveable: true, //能够移动
            rememberPos: true //记住移动的位置
        });
    });
    //5.修改admin
    $('[id = updateAdmin]').click(function() {
        var adminId = $(this).parent().parent().find('td').eq(0).html();
        findAdminById(adminId);
    });
};
//上一页 下一页按钮 搜索 重置 触发事件
$('#prePageBtn').click(function() {
    currentPage -= 1;
    if (currentPage < 1) {
        currentPage = 1;
        return;
    }
    PageFind();
});
$('#nextPageBtn').click(function() {
    currentPage += 1;
    if (currentPage > totalPage) {
        currentPage = totalPage;
        return;
    }
    PageFind();
});
$('#searchBtn').click(function() {
    PageFind();
});
$('#reSetBtn').click(function() {
    $('#adminName').val('');
    $('#roleId').val('0');
    $('#startTime').val('');
    $('#endTime').val('');
});
/* **************************增加用户************************* */
$('#addAdmin').click(function() {
    //1.清空内容
    $('#saveAccount').val('');
    $('#savePwd').val('');
    $('#saveStateId').val('0');
    $('#saveStateId').val('0');
    //2.弹出模态框
    $('#saveModal').modal({
        backdrop: 'static', //点击页面不隐藏
        show: true, //显示窗口
        moveable: true, //能够移动
        rememberPos: true //记住移动的位置
    });
});

//保存ajax
function saveAdminAjax() {
    //1.获取内容
    var saveAccount = $('#saveAccount').val();
    var savePwd = $('#savePwd').val();
    var saveStateId = $('#saveStateId').val();
    var saveRoleId = $('#saveRoleId').val();
    //2.限制
    if (saveAccount == '' || savePwd == '' || saveStateId == '0' || saveRoleId == '0') {
        layer.confirm('您有内容框没有数据,请重新填写!!!', {
            btn: ['确定'] //按钮
        });
        return;
    }
    //3.发送AJAX
    $.ajax({
        url: "admin/saveAdmin",
        type: "post",
        dataType: "JSON",
        data: {
            adminName: saveAccount,
            adminPwd: savePwd,
            adminState: saveStateId,
            roleId: saveRoleId
        },
        success: function(data) {
            if (data > 0) {
                //插入数据成功
                layer.confirm('添加管理员成功', {
                    btn: ['确定'] //按钮
                });
                $('#saveModal').modal('hide', 'fit');
                PageFind();
            }
        }
    });
}

/* **************************禁用启用************************** */
function banAjax(adminId, stateName) {
    //1.发送AJAX
    $.ajax({
        url: "admin/banPick",
        type: "post",
        dataType: "JSON",
        data: {
            adminId: adminId,
            stateName: stateName
        },
        success: function(data) {
            if (data > 0) {
                //插入数据成功
                layer.confirm('操作成功', {
                    btn: ['确定'] //按钮
                });
                PageFind();
            }
        }
    });
};
/* *****************************重置密码******************************* */
function updatePwdAjax() {
    //1.获取参数
    var rePwdAdminId = $('#rePwdAdminId').html();
    var rePwdPwd = $('#rePwdPwd').val();
    //2.限制
    if (rePwdPwd == '') {
        layer.confirm('请输入密码', {
            btn: ['确定'] //按钮
        });
        return;
    }
    //3.发送ajax
    $.ajax({
        url: 'admin/updatePwd',
        type: 'post',
        dataType: 'JSON',
        data: {
            adminId: rePwdAdminId,
            adminPwd: rePwdPwd
        },
        success: function(data) {
            if (data > 0) {
                //插入数据成功
                layer.confirm('修改密码成功', {
                    btn: ['确定'] //按钮
                });
                $('#rePwdModal').modal('hide', 'fit');
            }
        }
    });
};
/* **************************amdinId查詢Admin**************************** */
function findAdminById(adminId) {
    //1.ajax
    $.ajax({
        url: 'admin/findAdminByAdminId',
        type: 'post',
        dataType: 'JSON',
        data: {
            adminId: adminId
        },
        success: function(admin) {
            //1.赋值弹框
            $('#updateAdminId').html(admin.adminId);
            $('#updateAccount').val(admin.adminName);
            $('#updatePwd').val(admin.adminPwd);
            $('#updateStateId').val(admin.adminState);
            $('#updateRoleId').val(admin.roleId);
            //2.显示弹窗
            $('#updateModal').modal({
                backdrop: 'static', //点击页面不隐藏
                show: true, //显示窗口
                moveable: true, //能够移动
                rememberPos: true //记住移动的位置
            });
        }
    });
};
/* **************************更新用户**************************** */
function updateAdminInfo() {
    //1.获取参数
    var adminId = $('#updateAdminId').html();
    var account = $('#updateAccount').val();
    var pwd = $('#updatePwd').val();
    var stateId = $('#updateStateId').val();
    var roleId = $('#updateRoleId').val();
    //2.发送
    $.ajax({
        url: 'admin/updateAdminInfo',
        type: 'post',
        dataType: 'JSON',
        data: {
            adminId: adminId,
            adminName: account,
            adminPwd: pwd,
            adminState: stateId,
            roleId: roleId
        },
        success: function(data) {
            if (data > 0) {
                //插入数据成功
                layer.confirm('修改成功', {
                    btn: ['确定'] //按钮
                });
                $('#updateModal').modal('hide', 'fit');
                PageFind();
            }
        }
    });
}